import { postSaveForm, getForm, postDeleteForm, postSubmitForm } from './service';
import { queryImageFromOss } from '@/services/api';

export default {
  namespace: 'createCompany',
  state: {
    companyInformation: {},
  },

  effects: {
    *saveForm({ payload }, { call, put }) {
      const response = yield call(postSaveForm, payload);
      if (response.code === 1) {
        yield put({
          type: 'updateForm',
          payload,
        });
        return Promise.resolve(response.data);
      }
      return Promise.reject(response.msg);
    },
    *queryForm({ payload }, { call, put }) {
      const response = yield call(getForm, payload.temId);
      if (response.code === 1) {
        if (response.data) {
          const imgParams = {
            moduleCode: 'company',
            productLineId: 0,
            userId: 1,
          };

          let finalData = {
            ...response.data,
          };

          if (payload.step === 1) {
            // 获取公司logo图片地址
            const companyLogoResponse = yield call(queryImageFromOss, {
              ...imgParams,
              fileInfoId: response.data.companyLogo,
            });
            // 获取营业执照图片地址
            const businessLicenceIdResponse = yield call(queryImageFromOss, {
              ...imgParams,
              fileInfoId: response.data.businessLicenceId,
            });
            finalData.companyLogo = [
              {
                uid: response.data.companyLogo,
                name: companyLogoResponse.data.fileName,
                status: 'done',
                url: companyLogoResponse.data.downloadUrl,
                thumbUrl: companyLogoResponse.data.downloadUrl,
              },
            ];
            finalData.businessLicenceId = [
              {
                uid: response.data.businessLicenceId,
                name: businessLicenceIdResponse.data.fileName,
                status: 'done',
                url: businessLicenceIdResponse.data.downloadUrl,
                thumbUrl: businessLicenceIdResponse.data.downloadUrl,
              },
            ];
          }

          if (payload.step === 2) {
            if (response.data.legalPerFacePhoto) {
              // 获取法人身份证正面图片
              const legalPerFacePhotoResponse = yield call(queryImageFromOss, {
                ...imgParams,
                fileInfoId: response.data.legalPerFacePhoto,
              });
              finalData.legalPerFacePhoto = [
                {
                  uid: response.data.legalPerFacePhoto,
                  name: legalPerFacePhotoResponse.data.fileName,
                  status: 'done',
                  url: legalPerFacePhotoResponse.data.downloadUrl,
                  thumbUrl: legalPerFacePhotoResponse.data.downloadUrl,
                },
              ];
            }
            if (response.data.legalPerBackPhoto) {
              // 获取法人身份证反面图片
              const legalPerBackPhotoResponse = yield call(queryImageFromOss, {
                ...imgParams,
                fileInfoId: response.data.legalPerBackPhoto,
              });
              finalData.legalPerBackPhoto = [
                {
                  uid: response.data.legalPerBackPhoto,
                  name: legalPerBackPhotoResponse.data.fileName,
                  status: 'done',
                  url: legalPerBackPhotoResponse.data.downloadUrl,
                  thumbUrl: legalPerBackPhotoResponse.data.downloadUrl,
                },
              ];
            }
          }

          if (payload.step === 3) {
            if (response.data.authorizationId) {
              // 获取授权书
              const authorizationResponse = yield call(queryImageFromOss, {
                ...imgParams,
                fileInfoId: response.data.authorizationId,
              });
              finalData.authorizationId = [
                {
                  uid: response.data.authorizationId,
                  name: authorizationResponse.data.fileName,
                  status: 'done',
                  url: authorizationResponse.data.downloadUrl,
                  thumbUrl: authorizationResponse.data.downloadUrl,
                },
              ];
            }
          }

          yield put({
            type: 'updateForm',
            payload: finalData,
          });
          return Promise.resolve(finalData);
        } else {
          return Promise.reject('该公司暂存信息不存在');
        }
      }
      return Promise.reject(response.msg);
    },
    *submitForm({ payload }, { call }) {
      const response = yield call(postSubmitForm, payload);
      if (response.code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(response.msg);
    },
    *deleteForm({ payload }, { call }) {
      const response = yield call(postDeleteForm, payload);
      if (response.code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(response.msg);
    },
  },

  reducers: {
    updateForm(state, { payload }) {
      state.companyInformation = payload;
      return state;
    },
    removeLocalForm(state, _) {
      state.companyInformation = {};
      return state;
    },
  },
};
